package com.linx.common.core.request;

import com.alibaba.fastjson.util.TypeUtils;
import jakarta.servlet.http.HttpServletRequest;

import java.util.*;


public class PageData extends HashMap implements Map {

	private static final long serialVersionUID = 1L;

	Map map = null;
	HttpServletRequest request;

	public PageData(HttpServletRequest request) {
		this.request = request;
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[] values) {
                value = "";
				for (int i = 0; i < values.length; i++) {
					if (values.length >= 2) {
						value += values[i] + ",";
					} else {
						value = values[i] + ",";
					}
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		map = returnMap;
	}

	public PageData() {
		map = new HashMap();
	}

	@Override
	public Object get(Object key) {
		Object obj = null;
		if (map.get(key) instanceof Object[] arr) {
            obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
		} else {
			obj = map.get(key);
		}
		return obj;
	}

	public String getToString(Object key) {
		if(get(key)==null){
			return "";
		}
		return   get(key).toString();
	}

	public String getString(Object key) {


		Object o = get(key);
		if (o == null){
			return "";
		}else {
			return o.toString();
		}
	}

	public Long getLong(String key) {
		Object value = get(key);

		if (value == null){
			return null;
		}

		return Long.valueOf(String.valueOf(value));
	}
	@SuppressWarnings("unchecked")
	@Override
	public Object put(Object key, Object value) {
		return map.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return map.remove(key);
	}

    @Override
    public void clear() {
		map.clear();
	}

    @Override
    public boolean containsKey(Object key) {
		// TODO Auto-generated method stub
		return map.containsKey(key);
	}

    @Override
    public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return map.containsValue(value);
	}

    @Override
    public Set entrySet() {
		// TODO Auto-generated method stub
		return map.entrySet();
	}

    @Override
    public boolean isEmpty() {
		// TODO Auto-generated method stub
		return map.isEmpty();
	}

    @Override
    public Set keySet() {
		// TODO Auto-generated method stub
		return map.keySet();
	}

    @Override
    @SuppressWarnings("unchecked")
	public void putAll(Map t) {
		// TODO Auto-generated method stub
		map.putAll(t);
	}

    @Override
    public int size() {
		// TODO Auto-generated method stub
		return map.size();
	}

    @Override
    public Collection values() {
		return map.values();
	}

	public Integer getInteger(String key) {
		Object value = get(key);
		return TypeUtils.castToInt(value);
	}

	public Double getDouble(String key) {
		Object value = get(key);
		return TypeUtils.castToDouble(value);
	}
	public Double getToDouble(String key) {
		Object value = get(key);
        if (value == null || "".equals(value.toString())) {
			return 0d;
		}
		return TypeUtils.castToDouble(value);
	}

}
